<template>
  <div class="avatar">
    <img v-if="hasImage" :src="imageSrc" alt="User Avatar" />
    <span v-else>{{ initials }}</span>
  </div>
</template>
<script setup lang="ts">
import { computed } from "vue";
import { PropType } from "vue";
import httpRequest from "@/api/services";

// 定义 props
const props = defineProps({
  imageId: {
    type: String as PropType<string>,
    required: false,
    default: ""
  },
  username: {
    type: String as PropType<string>,
    required: true
  }
});
// 计算属性
const hasImage = computed(() => !!props.imageId);
const imageSrc = computed(() => httpRequest.downloadUrl() + props.imageId);
const initials = computed(() => {
  if (props.username) {
    return props.username.slice(-2);
  }
  return "";
});
</script>
<style scoped>
.avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  overflow: hidden;
  font-size: 14px;
  color: #ffffff;
  background-color: var(--el-color-primary);
  border-radius: 50%;
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}
</style>
